package com.dmai.capacity.platform.user.service.impl;

import com.dmai.capacity.platform.common.constant.CodeEnum;
import com.dmai.capacity.platform.common.constant.CommonConstant;
import com.dmai.capacity.platform.common.dto.PageResult;
import com.dmai.capacity.platform.common.dto.PageResultData;
import com.dmai.capacity.platform.common.model.LoginAppUser;
import com.dmai.capacity.platform.common.model.SysRole;
import com.dmai.capacity.platform.user.mapper.SysFunctionPermissionDao;
import com.dmai.capacity.platform.user.model.SysFunctionPermission;
import com.dmai.capacity.platform.user.service.ISysFunctionPermissionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;


@Service
public class SysFunctionPermissionServiceImpl implements ISysFunctionPermissionService {

    @Autowired
    private SysFunctionPermissionDao sysFunctionPermissionDao;

    /**
     * 添加
     *
     * @param sysFunctionPermission
     */
    public int save(SysFunctionPermission sysFunctionPermission) {
        return sysFunctionPermissionDao.save(sysFunctionPermission);
    }

    /**
     * 修改
     *
     * @param sysFunctionPermission
     */
    public int update(SysFunctionPermission sysFunctionPermission) {
        return sysFunctionPermissionDao.update(sysFunctionPermission);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return sysFunctionPermissionDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SysFunctionPermission findById(String id) {
        return sysFunctionPermissionDao.findById(id);
    }

    @Override
    public List<SysFunctionPermission> findByUser(LoginAppUser user) {
        List<SysFunctionPermission> permissions = new ArrayList<>();
        Map<String, Object> param = new HashMap<>();
        if (CollectionUtils.isNotEmpty(user.getRoles())) {
            for (SysRole role : user.getRoles()) {
                param.put("objectType", "ROLE");
                param.put("objectId", role.getId());
                permissions.addAll(sysFunctionPermissionDao.findAll(param));
            }
        }
        param.put("objectType", "USER");
        param.put("objectId", user.getId());
        permissions.addAll(sysFunctionPermissionDao.findAll(param));

        return permissions;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SysFunctionPermission> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT) != null && MapUtils.getInteger(params, CommonConstant.PAGE_SIZE) != null)
            PageHelper.startPage(MapUtils.getInteger(params, CommonConstant.PAGE_CURRENT), MapUtils.getInteger(params, CommonConstant.PAGE_SIZE), true);

        List<SysFunctionPermission> list = sysFunctionPermissionDao.findAll(params);
        PageInfo<SysFunctionPermission> pageInfo = new PageInfo(list);

        return PageResult.<SysFunctionPermission>builder().data(PageResultData.<SysFunctionPermission>builder().records(pageInfo.getList()).total(pageInfo.getTotal()).pages(pageInfo.getPages()).build()).code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SysFunctionPermission> findAll(Map<String, Object> params) {
        return sysFunctionPermissionDao.findAll(params);
    }

}
